﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Imaging;

namespace ComputerizedRehabForms
{
    public partial class BellsTestDisplay : Form
    {
        static int NUM_COLUMNS = 7;
        static int NUM_ROWS = 45;
        static float x_offset = 0.09f;
        float screen_height;
        float screen_width;
        float w_size_scale = 0.015f;
        float h_size_scale = 0.025f;
        PictureBox [,] lab_arr = new PictureBox [NUM_COLUMNS,NUM_ROWS];
        bool [,] bell_arr = new bool [NUM_COLUMNS, NUM_ROWS];
        Random rand = new Random();
        Random r = new Random();
        List<string> imageNames = new List<string>();
        List<BellData> bell_data = new List<BellData>();
        bool test_start = false;
        bool test_finish = false;
        int[] bells_found = new int [NUM_COLUMNS];

        public BellsTestDisplay()
        {
            InitializeComponent();
            imageNames.Add("Apple");
            imageNames.Add("Bird");
            imageNames.Add("Car");
            imageNames.Add("Cloud");
            imageNames.Add("Fish");
            imageNames.Add("Guitar");
            imageNames.Add("Gun");
            imageNames.Add("Handsaw");
            imageNames.Add("Horse");
            imageNames.Add("House");
            imageNames.Add("Key");
            imageNames.Add("Mallet");
            imageNames.Add("Teapot");
            imageNames.Add("Tree");
        }

        private void BellsTestDisplay_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void BellsTestDisplay_Load(object sender, EventArgs e)
        {
            generateTest();
        }

        private void Label_click(object sender, EventArgs e)
        {
            if (test_start)
            {
                int xloc = PointToClient(Cursor.Position).X;
                int yloc = PointToClient(Cursor.Position).Y;
                foreach (BellData belloc in bell_data)
                {
                    if (belloc.getX() - 10 < xloc && belloc.getX() + 30 > xloc && belloc.getY() - 10 < yloc && belloc.getY() + 30 > yloc && !belloc.isFound())
                    {
                        lab_arr[belloc.getI(), belloc.getJ()].Image = Image.FromFile("../Bells_Images/Bell_Green.jpg");
                        bells_found[belloc.getI()]++;
                        belloc.foundBell();
                    }
                }
            }
        }

        class BellData
        {
            public int i_val;
            public int j_val;
            public float x_val;
            public float y_val;
            public bool found;

            public BellData(int i, int j, float x, float y)
            {
                i_val = i;
                j_val = j;
                x_val = x;
                y_val = y;
                found = false;
            }

            public float getX() {
                return x_val;
            }

            public float getY()
            {
                return y_val;
            }

            public int getI()
            {
                return i_val;
            }

            public int getJ()
            {
                return j_val;
            }

            public bool isFound()
            {
                return found;
            }

            public void foundBell()
            {
                found = true;
            }
        }

        private void BellsTestDisplay_MouseClick(object sender, MouseEventArgs e)
        {
            if (test_start)
            {
                int xloc = PointToClient(Cursor.Position).X;
                int yloc = PointToClient(Cursor.Position).Y;
                foreach (BellData belloc in bell_data)
                {
                    if (belloc.getX() - 10 < xloc && belloc.getX() + 30 > xloc && belloc.getY() - 10 < yloc && belloc.getY() + 30 > yloc && !belloc.isFound())
                    {
                        lab_arr[belloc.getI(), belloc.getJ()].Image = Image.FromFile("../Bells_Images/Bell_Green.jpg");
                        bells_found[belloc.getI()]++;
                        belloc.foundBell();
                    }
                }
            }
        }

        private void BellsTestDisplay_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back && !test_start)
            {
                test_start = true;
            }
            else if (e.KeyCode == Keys.Back && !test_finish)
            {
                DataManager.resultImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
                DataManager.g = Graphics.FromImage(DataManager.resultImage);
                DataManager.g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
                Thread res_thread = new Thread(setResultsThread);
                test_finish = true;
                res_thread.Start();
                res_thread.Join();
                this.Controls.Clear();
                DataManager.hideBellsTest();
                DataManager.showBellsTestResults();
                generateTest();
            }
        }

        public void generateTest()
        {
            if (DataManager.getFormType() == 1)
            {
                generateStaticTest();
            }
            else
            {
                generateDynamicTest();
            }
        }

        private void generateDynamicTest()
        {
            test_start = false;
            test_finish = false;
            bell_data = new List<BellData>();

            for (int b = 0; b < NUM_COLUMNS; b++)
            {
                bells_found[b] = 0;
            }
            screen_height = this.Height;
            screen_width = this.Width;
            int bell_counter = 0;
            bool bell = false;

            for (int i = 0; i < NUM_COLUMNS; i++)
            {
                int total_bells = 0;

                for (int j = 0; j < NUM_ROWS; j++)
                {
                    bell = false;
                    int index = r.Next(imageNames.Count);
                    string randomString = imageNames[index];
                    lab_arr[i, j] = new PictureBox();

                    if (total_bells < 5 && bell_counter >= 5)
                    {
                        int val = rand.Next(0, bell_counter);
                        if (val > 3)
                        {
                            bell = true;
                            lab_arr[i, j].Image = Image.FromFile("../Bells_Images/Bell.jpg");
                            bell_arr[i, j] = true;
                            bell_counter = 0;
                            total_bells++;
                        }
                        else
                        {
                            lab_arr[i, j].Image = Image.FromFile("../Bells_Images/" + randomString + ".jpg");
                            bell_arr[i, j] = false;
                            bell_counter++;
                        }
                    }
                    else
                    {
                        lab_arr[i, j].Image = Image.FromFile("../Bells_Images/" + randomString + ".jpg");
                        bell_arr[i, j] = false;
                        bell_counter++;
                    }
                    float x_float = i;
                    float y_float = j;
                    float x_off = (float)((float)r.NextDouble() * x_offset * (float)(r.Next(0, 2) * 2 - 1));
                    float xsection = ((x_float + 1f) / (7.00f + 1f));
                    float ysection = ((y_float + 1f) / (45.00f + 1f));
                    xsection += x_off;
                    if (bell)
                    {
                        bell_data.Add(new BellData(i, j, xsection * screen_width, ysection * screen_height));
                    }
                    lab_arr[i, j].Location = new System.Drawing.Point((int)(xsection * screen_width), (int)(ysection * screen_height));
                    lab_arr[i, j].Size = new Size((int)Math.Round(screen_width * w_size_scale), (int)Math.Round(screen_height * h_size_scale));
                    this.Controls.Add(lab_arr[i, j]);
                    this.lab_arr[i, j].Click += new System.EventHandler(Label_click);
                    this.lab_arr[i, j].SizeMode = PictureBoxSizeMode.Zoom;
                }

                while (total_bells < 5)
                {
                    int rand_no = rand.Next(0, NUM_ROWS);
                    if (!bell_arr[i, rand_no])
                    {
                        this.lab_arr[i, rand_no].Image = Image.FromFile("../Bells_Images/Bell.jpg");
                        bell_data.Add(new BellData(i, rand_no, this.lab_arr[i, rand_no].Location.X, this.lab_arr[i, rand_no].Location.Y));
                        total_bells++;
                    }
                }
            }
        }

        private void generateStaticTest()
        {
            test_start = false;
            test_finish = false;
            bell_data = new List<BellData>();

            for (int b = 0; b < NUM_COLUMNS; b++)
            {
                bells_found[b] = 0;
            }

            screen_height = this.Height;
            screen_width = this.Width;
            bool bell = false;

            for (int i = 0; i < NUM_COLUMNS; i++)
            {
                for (int j = 0; j < NUM_ROWS; j++)
                {
                    bell = false;
                    int index = r.Next(imageNames.Count);
                    string randomString = imageNames[index];
                    lab_arr[i, j] = new PictureBox();
                    switch (i)
                    {
                        case 0:
                            if (j == 3 || j == 15 || j == 19 || j == 27 || j == 35)
                            {
                                bell = true;
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/Bell.jpg");
                                bell_arr[i, j] = true;
                            }
                            else
                            {
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/" + randomString + ".jpg");
                                bell_arr[i, j] = false;
                            }
                            break;

                        case 1:
                            if (j == 1 || j == 9 || j == 25 || j == 31 || j == 37)
                            {
                                bell = true;
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/Bell.jpg");
                                bell_arr[i, j] = true;
                            }
                            else
                            {
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/" + randomString + ".jpg");
                                bell_arr[i, j] = false;
                            }
                            break;

                        case 2:
                            if (j == 10 || j == 18 || j == 23 || j == 31 || j == 35)
                            {
                                bell = true;
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/Bell.jpg");
                                bell_arr[i, j] = true;
                            }
                            else
                            {
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/" + randomString + ".jpg");
                                bell_arr[i, j] = false;
                            }
                            break;

                        case 3:
                            if (j == 5 || j == 11 || j == 23 || j == 29 || j == 31)
                            {
                                bell = true;
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/Bell.jpg");
                                bell_arr[i, j] = true;
                            }
                            else
                            {
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/" + randomString + ".jpg");
                                bell_arr[i, j] = false;
                            }
                            break;

                        case 4:
                            if (j == 7 || j == 21 || j == 25 || j == 33 || j == 39)
                            {
                                bell = true;
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/Bell.jpg");
                                bell_arr[i, j] = true;
                            }
                            else
                            {
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/" + randomString + ".jpg");
                                bell_arr[i, j] = false;
                            }
                            break;

                        case 5:
                            if (j == 7 || j == 15 || j == 23 || j == 31 || j == 37)
                            {
                                bell = true;
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/Bell.jpg");
                                bell_arr[i, j] = true;
                            }
                            else
                            {
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/" + randomString + ".jpg");
                                bell_arr[i, j] = false;
                            }
                            break;

                        case 6:
                            if (j == 5 || j == 9 || j == 19 || j == 29 || j == 33)
                            {
                                bell = true;
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/Bell.jpg");
                                bell_arr[i, j] = true;
                            }
                            else
                            {
                                lab_arr[i, j].Image = Image.FromFile("../Bells_Images/" + randomString + ".jpg");
                                bell_arr[i, j] = false;
                            }
                            break;
                    }
                    float x_float = i;
                    float y_float = j;
                    float x_off = (float)((float)r.NextDouble() * x_offset * (float)(r.Next(0, 2) * 2 - 1));
                    float xsection = ((x_float + 1f) / (7.00f + 1f));
                    float ysection = ((y_float + 1f) / (45.00f + 1f));
                    xsection += x_off;
                    if (bell)
                    {
                        bell_data.Add(new BellData(i, j, xsection * screen_width, ysection * screen_height));
                    }
                    lab_arr[i, j].Location = new System.Drawing.Point((int)(xsection * screen_width), (int)(ysection * screen_height));
                    lab_arr[i, j].Size = new Size((int)Math.Round(screen_width * w_size_scale), (int)Math.Round(screen_height * h_size_scale));
                    this.Controls.Add(lab_arr[i, j]);
                    this.lab_arr[i, j].Click += new System.EventHandler(Label_click);
                    this.lab_arr[i, j].SizeMode = PictureBoxSizeMode.Zoom;
                }
            }
        }

        public void setResultsThread()
        {
            DataManager.setBellsResults(bells_found);
        }

        public void generateTestThread()
        {
            generateTest();
        }
    }
}
